/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.NonexistentEntityException;
import controlador.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidad.Platillo;
import entidad.Comanda;
import entidad.ComandaHasPlatillo;
import entidad.ComandaHasPlatilloPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author a
 */
public class ComandaHasPlatilloJpaController implements Serializable {

    public ComandaHasPlatilloJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ComandaHasPlatillo comandaHasPlatillo) throws PreexistingEntityException, Exception {
        if (comandaHasPlatillo.getComandaHasPlatilloPK() == null) {
            comandaHasPlatillo.setComandaHasPlatilloPK(new ComandaHasPlatilloPK());
        }
        comandaHasPlatillo.getComandaHasPlatilloPK().setComandaidComanda(comandaHasPlatillo.getComanda().getIdComanda());
        comandaHasPlatillo.getComandaHasPlatilloPK().setPlatilloidPlatillo(comandaHasPlatillo.getPlatillo().getIdPlatillo());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Platillo platillo = comandaHasPlatillo.getPlatillo();
            if (platillo != null) {
                platillo = em.getReference(platillo.getClass(), platillo.getIdPlatillo());
                comandaHasPlatillo.setPlatillo(platillo);
            }
            Comanda comanda = comandaHasPlatillo.getComanda();
            if (comanda != null) {
                comanda = em.getReference(comanda.getClass(), comanda.getIdComanda());
                comandaHasPlatillo.setComanda(comanda);
            }
            em.persist(comandaHasPlatillo);
            if (platillo != null) {
                platillo.getComandaHasPlatilloList().add(comandaHasPlatillo);
                platillo = em.merge(platillo);
            }
            if (comanda != null) {
                comanda.getComandaHasPlatilloList().add(comandaHasPlatillo);
                comanda = em.merge(comanda);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findComandaHasPlatillo(comandaHasPlatillo.getComandaHasPlatilloPK()) != null) {
                throw new PreexistingEntityException("ComandaHasPlatillo " + comandaHasPlatillo + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ComandaHasPlatillo comandaHasPlatillo) throws NonexistentEntityException, Exception {
        comandaHasPlatillo.getComandaHasPlatilloPK().setComandaidComanda(comandaHasPlatillo.getComanda().getIdComanda());
        comandaHasPlatillo.getComandaHasPlatilloPK().setPlatilloidPlatillo(comandaHasPlatillo.getPlatillo().getIdPlatillo());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ComandaHasPlatillo persistentComandaHasPlatillo = em.find(ComandaHasPlatillo.class, comandaHasPlatillo.getComandaHasPlatilloPK());
            Platillo platilloOld = persistentComandaHasPlatillo.getPlatillo();
            Platillo platilloNew = comandaHasPlatillo.getPlatillo();
            Comanda comandaOld = persistentComandaHasPlatillo.getComanda();
            Comanda comandaNew = comandaHasPlatillo.getComanda();
            if (platilloNew != null) {
                platilloNew = em.getReference(platilloNew.getClass(), platilloNew.getIdPlatillo());
                comandaHasPlatillo.setPlatillo(platilloNew);
            }
            if (comandaNew != null) {
                comandaNew = em.getReference(comandaNew.getClass(), comandaNew.getIdComanda());
                comandaHasPlatillo.setComanda(comandaNew);
            }
            comandaHasPlatillo = em.merge(comandaHasPlatillo);
            if (platilloOld != null && !platilloOld.equals(platilloNew)) {
                platilloOld.getComandaHasPlatilloList().remove(comandaHasPlatillo);
                platilloOld = em.merge(platilloOld);
            }
            if (platilloNew != null && !platilloNew.equals(platilloOld)) {
                platilloNew.getComandaHasPlatilloList().add(comandaHasPlatillo);
                platilloNew = em.merge(platilloNew);
            }
            if (comandaOld != null && !comandaOld.equals(comandaNew)) {
                comandaOld.getComandaHasPlatilloList().remove(comandaHasPlatillo);
                comandaOld = em.merge(comandaOld);
            }
            if (comandaNew != null && !comandaNew.equals(comandaOld)) {
                comandaNew.getComandaHasPlatilloList().add(comandaHasPlatillo);
                comandaNew = em.merge(comandaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ComandaHasPlatilloPK id = comandaHasPlatillo.getComandaHasPlatilloPK();
                if (findComandaHasPlatillo(id) == null) {
                    throw new NonexistentEntityException("The comandaHasPlatillo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ComandaHasPlatilloPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ComandaHasPlatillo comandaHasPlatillo;
            try {
                comandaHasPlatillo = em.getReference(ComandaHasPlatillo.class, id);
                comandaHasPlatillo.getComandaHasPlatilloPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The comandaHasPlatillo with id " + id + " no longer exists.", enfe);
            }
            Platillo platillo = comandaHasPlatillo.getPlatillo();
            if (platillo != null) {
                platillo.getComandaHasPlatilloList().remove(comandaHasPlatillo);
                platillo = em.merge(platillo);
            }
            Comanda comanda = comandaHasPlatillo.getComanda();
            if (comanda != null) {
                comanda.getComandaHasPlatilloList().remove(comandaHasPlatillo);
                comanda = em.merge(comanda);
            }
            em.remove(comandaHasPlatillo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ComandaHasPlatillo> findComandaHasPlatilloEntities() {
        return findComandaHasPlatilloEntities(true, -1, -1);
    }

    public List<ComandaHasPlatillo> findComandaHasPlatilloEntities(int maxResults, int firstResult) {
        return findComandaHasPlatilloEntities(false, maxResults, firstResult);
    }

    private List<ComandaHasPlatillo> findComandaHasPlatilloEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ComandaHasPlatillo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ComandaHasPlatillo findComandaHasPlatillo(ComandaHasPlatilloPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ComandaHasPlatillo.class, id);
        } finally {
            em.close();
        }
    }

    public int getComandaHasPlatilloCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ComandaHasPlatillo> rt = cq.from(ComandaHasPlatillo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
